package edu.cmu.arch.pipesandfilters;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import edu.cmu.arch.pipesandfilters.frame.Variable;

/**
* File:SystemB.java
* Course: 17655
* Project: Assignment 1
* Group: Group 1
* @author Michael Azzopardi
* Versions:
*	1.0 February 2012
*
* This is the main program for the implementation of the Pipe-and-Filter network for System B
* 
*/
public class SystemB {

	/**
	 * @param args
	 * @author Michael Azzopardi
	 * 
	 */
	public static void main(String[] args) {
		System.out.println("Initializing System B Pipe-and-Filter Network");
		System.out.println("developed by Group 1");
		System.out.println("=============================================");
		
		// Instantiate fileSinkFilter
		try {		
			
			// Define file names default - replace filenames with parameters
			String sourceFilename = "DataSets" + File.separator + "FlightData.dat";
			String outputFilename = "DataSets" + File.separator + "OutputB.dat";
			String wildOutputFilename = "DataSets" + File.separator + "WildPoint.dat";
			
			if (args.length > 0) {
				if (args[1] != null) {
					sourceFilename = args[0];
				} else {
					System.out.println("Missing input filename parameter");
				}
				
				if (args[1] != null) {
					outputFilename = args[1];
				} else {
					System.out.println("Missing output filename parameter");
				}
			
				if (args[2] != null) {
					wildOutputFilename = args[2];
				} else {
					System.out.println("Missing wildpoint output filename parameter");
				}
						
			}
			System.out.println("Starting System B with input "+sourceFilename+" and output "+outputFilename+" and wildpoint output "+wildOutputFilename);		
			

			// DefineFilters
			SourceFilter sourceFilter;
			StripFilter stripFilter;
			StripFilter wildpointStripFilter;
			ConversionFilter tempConversionFilter;
			ConversionFilter altConversionFilter;
			ExtrapolateFilter extrapolateFilter;
			FileSinkFilter sinkFilter;
			FileSinkFilter wildpointSinkFilter;
						
			// Instantiate sourceFilter
			sourceFilter = new SourceFilter(sourceFilename);
			ArrayList<Variable> stripMeasurements = new ArrayList<Variable>();
			stripMeasurements.add(Variable.Time);
			stripMeasurements.add(Variable.Temperature);
			stripMeasurements.add(Variable.Altitude);
			stripMeasurements.add(Variable.Pressure);
			stripFilter = new StripFilter(stripMeasurements);
			extrapolateFilter = new ExtrapolateFilter(Variable.Pressure, 50D, 80D);
			// Instantiate sinkFilter and set fileName, header and separator
			sinkFilter = new FileSinkFilter( stripMeasurements,outputFilename);
			ArrayList<Variable> wildpointStripMeasurements = new ArrayList<Variable>();
			wildpointStripMeasurements.add(Variable.Time);
			wildpointStripMeasurements.add(Variable.Pressure);
			wildpointStripFilter = new StripFilter(wildpointStripMeasurements);
			wildpointSinkFilter = new FileSinkFilter(wildpointStripMeasurements, wildOutputFilename);				
			tempConversionFilter = new ConversionFilter(Variable.Temperature);
			altConversionFilter = new ConversionFilter(Variable.Altitude);			

			// Connect filters
			stripFilter.Connect(sourceFilter);
			tempConversionFilter.Connect(stripFilter);		
			altConversionFilter.Connect(tempConversionFilter);
			extrapolateFilter.Connect(altConversionFilter);						
			sinkFilter.Connect(extrapolateFilter.getOutputFilter1());
			wildpointStripFilter.Connect(extrapolateFilter.getOutputFilter2());
			wildpointSinkFilter.Connect(wildpointStripFilter);
			
			// Start sourceFilter
			sourceFilter.start();
			stripFilter.start();
			tempConversionFilter.start();
			altConversionFilter.start();
			extrapolateFilter.start();
			sinkFilter.start();
			wildpointStripFilter.start();
			wildpointSinkFilter.start();			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Error : "+e.getMessage());
			e.printStackTrace();
		}				
	}

}

		
